package com.xijia.wuyouwallet.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.adapter.ServerAdapter;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.ServerData;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class ServeActivity extends BaseActivity implements View.OnClickListener{

    private ImageView serve_back;
    //private LinearLayout serve_qq1_rl,serve_qq2_rl,serve_qq3_rl;
    //private ProgressBar serve_progressBar;
    //private WebView serve_wv;

    private ListView serve_lv;
    private List<String> list = new ArrayList<>();
    private ServerAdapter mAdapter;

    private LinearLayout serve_phone_ll;
    private TextView serve_phone_num;

    private String phoneNum = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serve);
        initView();
        getServe();
    }

    private void initView(){
        serve_back = (ImageView) findViewById(R.id.serve_back);
        serve_back.setOnClickListener(this);
        /*serve_qq1_rl = (LinearLayout) findViewById(R.id.serve_qq1_rl);
        serve_qq1_rl.setOnClickListener(this);
        serve_qq2_rl = (LinearLayout) findViewById(R.id.serve_qq2_rl);
        serve_qq2_rl.setOnClickListener(this);
        serve_qq3_rl = (LinearLayout) findViewById(R.id.serve_qq3_rl);
        serve_qq3_rl.setOnClickListener(this);*/



        /*serve_progressBar = (ProgressBar) findViewById(R.id.serve_progressBar);
        serve_wv = (WebView) findViewById(R.id.serve_wv);
        serve_wv.getSettings().setJavaScriptEnabled(true);
        //设置滚动条可见
        setProgressBarIndeterminateVisibility(true);
        init();
        //serve_wv.addJavascriptInterface(this, "getUser");
        serve_wv.loadUrl(UrlConfig.CUSTOM_SERVE_URL);*/

        serve_lv = (ListView) findViewById(R.id.serve_lv);
        serve_phone_ll = (LinearLayout) findViewById(R.id.serve_phone_ll);
        serve_phone_ll.setOnClickListener(this);
        serve_phone_num = (TextView) findViewById(R.id.serve_phone_num);

        mAdapter = new ServerAdapter(list,this);
        serve_lv.setAdapter(mAdapter);
        serve_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String url11 = "mqqwpa://im/chat?chat_type=wpa&uin=" + list.get(i) + "&version=1";
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url11)));
                } catch (Exception e) {
                    // 未安装手Q或安装的版本不支持    showToast("未安装手Q或安装的版本不支持");
                    showToast("您未安装QQ客户端或安装版本不支持！");
                }
            }
        });
    }


    /*private void init() {
        // TODO 自动生成的方法存根
        serve_wv.setWebViewClient(new WebViewClient(){
            //覆写shouldOverrideUrlLoading实现内部显示网页
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO 自动生成的方法存根
                view.loadUrl(url);
                return true;
            }
        });
        serve_wv.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根
                serve_progressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    serve_progressBar.setVisibility(View.GONE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }*/

    private void getServe(){
        XUtil.Post(UrlConfig.GETQQ_URL,null,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("获取客服result="+result);
                ServerData data = JSONObject.parseObject(result,ServerData.class);
                if (data.getCode()==200){
                    for (int i = 0;i < data.getData().getContact_qq_list().size();i++){
                        list.add(data.getData().getContact_qq_list().get(i));
                    }
                    phoneNum = data.getData().getContact_tel();
                    if (phoneNum==null){
                        serve_phone_ll.setVisibility(View.GONE);
                    }else {
                        serve_phone_num.setText(phoneNum);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取客服失败");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.serve_back:
                finish();
                break;
            case R.id.serve_phone_ll:
                if (phoneNum.equals("")){
                    showToast("网络异常，请重试");
                    return;
                }
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + phoneNum);
                intent.setData(data);
                startActivity(intent);
                break;
            /*case R.id.serve_qq1_rl:

                break;
            case R.id.serve_qq2_rl:

                break;
            case R.id.serve_qq3_rl:

                break;*/
        }
    }
}
